﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasCountryDAL : IMasCountryDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasCountryDAL()
        {

        }
        public DataSet LoadMasCountries(MasCountryInfo ctInfo)
        {
            string strSQL;
            Nullable<int> zoneSeq = ctInfo.CountryZones == null ? 0 : ctInfo.CountryZones.Sequence;

            DataSet ds = null;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", ctInfo.Sequence);
                dbManager.AddParameters(2, "@Zone", zoneSeq);

                strSQL = "SELECT c.*"
                            + ", z.ZONE_NAME"
                       + " FROM MAS_COUNTRIES c"
                       + " LEFT JOIN MAS_COUNTRY_ZONES z ON c.ZONE_SEQ = z.ZONE_SEQ"
                       + " WHERE c.RECORD_STATUS not like @DeleteRecord"
                       + (ctInfo.Sequence > 0 ? " AND c.COUNTRY_SEQ = @Sequence " : "")
                       + (zoneSeq > 0 ? " AND c.ZONE_SEQ = @Zone " : "");

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                return ds;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public IList<MasCountryInfo> ListMasCountries(MasCountryInfo ctInfo)
        {
            string strSQL;
            Nullable<int> zoneSeq = ctInfo.CountryZones == null ? 0 : ctInfo.CountryZones.Sequence;

            IList<MasCountryInfo> infoList = new List<MasCountryInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", ctInfo.Sequence);
                dbManager.AddParameters(2, "@Zone", zoneSeq);

                strSQL = "SELECT COUNTRY_SEQ, COUNTRY_CODE, COUNTRY_NAME, COUNTRY_NAME_ENG, ZONE_SEQ"
                            +", REMARKS, RECORD_STATUS, LOCAL"
                       + " FROM MAS_COUNTRIES"
                       + " WHERE RECORD_STATUS not like @DeleteRecord"
                       + (ctInfo.Sequence > 0 ? " AND COUNTRY_SEQ = @Seq " : "")
                       + (zoneSeq > 0 ? " AND ZONE_SEQ = @Zone " : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasCountryZoneDAL zoneDAL = new MasCountryZoneDAL();
                    MasCountryZoneInfo zoneInfo = new MasCountryZoneInfo();
                    if (!rdr.IsDBNull(4))
                    {
                        IList<MasCountryZoneInfo> zoneList = zoneDAL.ListMasCountryZones(new MasCountryZoneInfo(rdr.IsDBNull(4) ? 0 : rdr.GetInt32(4)));
//// Tiger Error: why you know that zoneList have element. you don't have any constaint in database                        
                        zoneInfo = zoneList[0];
                    }

                    MasCountryInfo Info = new MasCountryInfo(rdr.GetInt32(0)
                           , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                           , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                           , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                           , zoneInfo
                           , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                           , 0, 0);
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Insert(MasCountryInfo Info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@Local", Info.Local);
                dbManager.AddParameters(4, "@ZoneSeq", Info.CountryZones.Sequence);
                dbManager.AddParameters(5, "@Remarks", Info.Remarks);
                dbManager.AddParameters(6, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(8, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_COUNTRIES(COUNTRY_CODE, COUNTRY_NAME, COUNTRY_NAME_ENG, LOCAL"
                        + ", ZONE_SEQ, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Code, @Name, @NameEng, @Local, @ZoneSeq, @Remarks, @RecordStatus"
                        + ", @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }            
        }

        public void Update(MasCountryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@Code", Info.Code);
                dbManager.AddParameters(2, "@Name", Info.Name);
                dbManager.AddParameters(3, "@NameEng", Info.NameEng);
                dbManager.AddParameters(4, "@Local", Info.Local);
                dbManager.AddParameters(5, "@ZoneSeq", Info.CountryZones.Sequence);
                dbManager.AddParameters(6, "@Remarks", Info.Remarks);
                dbManager.AddParameters(7, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_COUNTRIES"
                    + " SET COUNTRY_CODE = @Code"
                        + ", COUNTRY_NAME = @Name"
                        + ", COUNTRY_NAME_ENG = @NameEng"
                        + ", LOCAL = @Local"
                        + ", ZONE_SEQ = @ZoneSeq"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE COUNTRY_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(MasCountryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Info.Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", Info.UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_COUNTRIES"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE COUNTRY_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void UpdateZones(MasCountryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", Info.Sequence);
                dbManager.AddParameters(1, "@zone_seq", Info.CountryZones.Sequence);
                dbManager.AddParameters(2, "@updated_by", Info.UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE MAS_COUNTRIES"
                    + " SET ZONE_SEQ = @zone_seq"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE COUNTRY_SEQ = @seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
